<--- %%NOBANNER%% --> _rant.sas
 BackForward

/*-------------------<-- Start of Description -->--------------------\
| Generate random variate from a T distribution                      |
|--------------------<--- End of Description -->---------------------|
|--------------------------------------------------------------------|
|--------------<--- Start of Files or Arguments Needed -->-----------|
| Arguments Need:                                                    |
|      seed - seed; Required, default is the current system time;    |
|      var  - the variable to save the generated random variates;    |
|      df   - the degree of freedom;                                 |
|      mean - the mean of the normal distribution;                   |
|      std  - the std of the distribution;                           |
|      temp - temporary seed variable;                               |
|---------------<--- End of Files or Arguments Needed -->------------|
|--------------------------------------------------------------------|
|----------------<--- Start of Example and Usage -->-----------------|
|Example                                                             |
|   data one;                                                        |
|      do i=1 to 20;                                                 |
|      x=%_rant(seed=1, df=3);                                       |
|      z=%_rant(seed=1, mean=10, std=5, df=3);                       |
|      %_rant(seed=12345,var=y, mean=10, std=1, df=3);               |
|      output;                                                       |
|      end;                                                          |
|   run; %print(one);                                                |
|Usage: _rant(seed=%sysfunc(datetime(), 15.), df=REQUIRED, var=,     |
|             mean=, std=);                                          |
\-------------------<--- End of Example and Usage -->---------------*/
%macro _rant(seed=%sysfunc(datetime(), 15.), df=REQUIRED, var=, mean=, 
             std=, temp=_rant0_);
/*--------------------------------------------\
| Author:  Duo Zhou;                          |
| Created: 3-22-2002 6:30pm;                  |
| Purpose: Random T Generator;                |
\--------------------------------------------*/
%let _dfchk_=%sysfunc(rxmatch(%sysfunc(rxparse($a|$A|$w)),&df)); 
%if (%quote(&seed) eq) or &_dfchk_ %then %do;
   %if (%quote(&seed) eq) %then %do;
      %put ==> Error: This is not a valid seed!; 
      %if (%length(&var)) %then %do; &var=.; %end;
      %else %do; .;%end;
   %end;
   %if &_dfchk_ %then %do;
      %put ==> Error: &df is not a valid degree of freedom!; 
      %if (%length(&var)) %then %do; &var=.; %end;
      %else %do; .;%end;
   %end;
   %goto finish;
%end;
%else %do;
   %if (%length(&var)) %then %do;
      %if (not %sysfunc(rxmatch(%sysfunc(rxparse(_|.|$a|$A|$w)),&seed))) %then %do;
         drop &temp;
         retain &temp &seed;
         %let seed=&temp;
      %end;
      call ranuni(&seed, &var);
      &var=tinv(&var, &df);
   %end;
   %else tinv(ranuni(&seed), &df);
   %if (%length(&std))  %then *&std;
   %if (%length(&mean)) %then +&mean;
%end;
%finish:
%mend _rant;